﻿using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using SmartFramework.Core.Extensions;
using SmartFramework.Core.Utils;

namespace SmartFramework.Core.Plugin.Pages {
	public abstract class BasicPage : Page {
		protected override void FrameworkInitialize() {
			base.FrameworkInitialize();
			base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
			this.EAddLiteral("<html xmlns:ts>\n");
			var head = new HtmlHead();
			InitHead(head);
			Controls.Add(head);
			if (BodyScrollable) {
				this.EAddLiteral("<body>");
			} else {
				this.EAddLiteral("<body scroll=\"no\">");
			}
			var form = new HtmlForm();
			InitForm(form);
			Controls.Add(form);

			this.EAddLiteral("</body></html>");
		}

		protected abstract void InitForm(HtmlForm form);

		protected virtual void InitHead(HtmlHead head) {
			head.Title = "MyTitle";
			head.EAddLiteral("<base target=\"_self\"/>");
		}

		public bool BodyScrollable { get; set; }

		protected override void SavePageStateToPersistenceMedium(object state)
		{
			if (!EnableViewState)
				base.SavePageStateToPersistenceMedium(state);
				using(var fs = new FileStream( FS.GetCurrentDirectory()+"\\ViewStates\\abc.vs", FileMode.Create))
			  {
					var formatter = new LosFormatter();
					formatter.Serialize(fs, state);
				}
		}
	}
}
